package org.dromara.book.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyReaderTypeBo;
import org.dromara.book.domain.vo.SzlyReaderTypeVo;
import org.dromara.book.domain.SzlyReaderType;
import org.dromara.book.mapper.SzlyReaderTypeMapper;
import org.dromara.book.service.ISzlyReaderTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 读者类型Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-20
 */
@RequiredArgsConstructor
@Service
public class SzlyReaderTypeServiceImpl implements ISzlyReaderTypeService {

    private final SzlyReaderTypeMapper baseMapper;

    /**
     * 查询读者类型
     */
    @Override
    public SzlyReaderTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询读者类型列表
     */
    @Override
    public TableDataInfo<SzlyReaderTypeVo> queryPageList(SzlyReaderTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SzlyReaderType> lqw = buildQueryWrapper(bo);
        Page<SzlyReaderTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询读者类型列表
     */
    @Override
    public List<SzlyReaderTypeVo> queryList(SzlyReaderTypeBo bo) {
        LambdaQueryWrapper<SzlyReaderType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SzlyReaderType> buildQueryWrapper(SzlyReaderTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyReaderType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SzlyReaderType::getName, bo.getName());
        lqw.eq(bo.getBorrowNum() != null, SzlyReaderType::getBorrowNum, bo.getBorrowNum());
        lqw.eq(bo.getBorrowLimit() != null, SzlyReaderType::getBorrowLimit, bo.getBorrowLimit());
        lqw.eq(bo.getIfAdvance() != null, SzlyReaderType::getIfAdvance, bo.getIfAdvance());
        lqw.eq(bo.getAdvanceNum() != null, SzlyReaderType::getAdvanceNum, bo.getAdvanceNum());
        lqw.eq(bo.getAdvanceLimit() != null, SzlyReaderType::getAdvanceLimit, bo.getAdvanceLimit());
        lqw.eq(bo.getIfRenew() != null, SzlyReaderType::getIfRenew, bo.getIfRenew());
        lqw.eq(bo.getRenewNum() != null, SzlyReaderType::getRenewNum, bo.getRenewNum());
        lqw.eq(bo.getRenewLimit() != null, SzlyReaderType::getRenewLimit, bo.getRenewLimit());
        return lqw;
    }

    /**
     * 新增读者类型
     */
    @Override
    public Boolean insertByBo(SzlyReaderTypeBo bo) {
        SzlyReaderType add = MapstructUtils.convert(bo, SzlyReaderType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改读者类型
     */
    @Override
    public Boolean updateByBo(SzlyReaderTypeBo bo) {
        SzlyReaderType update = MapstructUtils.convert(bo, SzlyReaderType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyReaderType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除读者类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
